package com.cloudxingkong.dianyinke.NewTeacher.Ui;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudxingkong.dianyinke.NewTeacher.Utils.WordWrapView;
import com.cloudxingkong.dianyinke.R;
import com.nostra13.universalimageloader.core.ImageLoader;

public class UWDetail extends AppCompatActivity {
	private String lessonID, coverPath, lessonTitle, lessonPrice, lessonLabel, lessonAddr, lessonNote;
	private ImageView ivCover;
	private TextView tvNote, tvAddr, tvPrice, tvTitle;
	private WordWrapView wordWrapView;
	private SharedPreferences sharedPreferences;

	@Override
	protected void onCreate (Bundle savedInstanceState) {
		super.onCreate (savedInstanceState);
		setContentView (R.layout.activity_uwdetail);
		initView ();
		loadData ();


		String[] split = lessonLabel.split (",");
		for (int i = 0; i < split.length; i++) {
			TextView tv = new TextView (this);
			tv.setGravity (Gravity.CENTER);
			tv.setTextColor (getResources ().getColor (R.color.white));
			tv.setBackgroundResource (R.drawable.shape_uw_label_main);
			tv.setText (split[i]);
			if (wordWrapView.getChildCount () < split.length) {
				wordWrapView.addView (tv);
			}
		}

		loadCover ();
	}

	private void loadCover () {
		ImageLoader.getInstance ().displayImage (coverPath, ivCover);
	}

	private void loadData () {
		tvTitle.setText (lessonTitle);
		tvPrice.setText ("￥ " + lessonPrice);
		tvAddr.setText (lessonAddr);
		tvNote.setText (lessonNote);
	}

	private void initView () {
		wordWrapView = (WordWrapView) findViewById (R.id.uw_detail_wwv);
		ivCover = (ImageView) findViewById (R.id.uw_detail_cover);
		tvTitle = (TextView) findViewById (R.id.uw_detail_title);
		tvPrice = (TextView) findViewById (R.id.uw_detail_price);
		tvAddr = (TextView) findViewById (R.id.uw_detail_address);
		tvNote = (TextView) findViewById (R.id.uw_detail_note);
		Intent intent = getIntent ();
		lessonID = intent.getStringExtra ("lessonID");
		coverPath = intent.getStringExtra ("coverPath");
		lessonTitle = intent.getStringExtra ("lessonTitle");
		lessonPrice = intent.getStringExtra ("lessonPrice");
		lessonLabel = intent.getStringExtra ("lessonLabel");
		lessonAddr = intent.getStringExtra ("lessonAddr");
		lessonNote = intent.getStringExtra ("lessonNote");
		sharedPreferences = getSharedPreferences ("data_dyk", 0);
		SharedPreferences.Editor edit = sharedPreferences.edit ();
		edit.putString ("lessonTitle", lessonTitle);
		edit.putString ("lessonPrice", lessonPrice);
		edit.putString ("lessonAddr", lessonAddr);
		edit.putString ("lessonNote", lessonNote);
		edit.putString ("lessonLabel", lessonLabel);
		edit.putString ("lessonID", lessonID);
		edit.commit ();


	}

	public void back (View view) {
		finish ();
	}


	public void updataLesson (View view) {
		Toast.makeText (UWDetail.this, "更新数据", Toast.LENGTH_SHORT).show ();
		Intent intent = new Intent (this, UploadingWork.class);
		intent.putExtra ("updataLesson", "101");
		startActivity (intent);

	}
}
